package com.lg.teacherCrud.service;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lg.teacherCrud.mapper.TeacherMapper;
import com.lg.teacherCrud.entity.Teacher;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.time.LocalDate;
import java.util.List;
import java.util.Random;

@Service
public class TeacherService extends ServiceImpl<TeacherMapper, Teacher> {

    // 批量插入教师
    public void batchInsertTeachers() {
        Random random = new Random();
        for (int i = 1; i <= 500; i++) {
            Teacher teacher = new Teacher();
            teacher.setName("Teacher" + i);
            teacher.setCourse("Course" + random.nextInt(10));
            teacher.setBirthday(Date.valueOf(LocalDate.of(1980, 1, 1).plusDays(random.nextInt(10000))));
            this.save(teacher);  // 使用MyBatis-Plus的save方法插入记录
        }
    }

    // 获取倒数第二条记录
    public Teacher getSecondLastTeacher() {
        List<Teacher> teachers = this.list();  // 获取所有教师
        return teachers.size() >= 2 ? teachers.get(teachers.size() - 2) : null;
    }
}